/*
* $Id: TableRegion.cpp 50 2010-03-03 15:11:54Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "TableRegion.h"
#include "ViewDefines.h"
#include "../JournalDefined.h"

CTableRegion::CTableRegion()
: m_bHScroll(false), m_bVScroll(false){}
CTableRegion::~CTableRegion(){}

void CTableRegion::SetRect(CRect rect){
	m_rectRegion = rect;
}

void CTableRegion::Create(CWnd* pParent){
	m_pParent = pParent;
	m_HScroll.Create(WS_CHILD | WS_VISIBLE | SBS_HORZ, CRect(), pParent, HSCROLL_ID);
	m_VScroll.Create(WS_CHILD | WS_VISIBLE | SBS_VERT, CRect(), pParent, VSCROLL_ID);
	m_tableDrawer.InitTable();
}

void CTableRegion::Paint(CDC* dc){
	UpgradeScroll();
	CRect rect(m_rectRegion);
	if(m_bVScroll)
		rect.right -= SIZE_SCROLL;
	if(m_bHScroll)
		rect.bottom -= SIZE_SCROLL;
	m_tableDrawer.PaintTable(dc, rect);
	if(m_bHScroll && m_bVScroll){
		CBrush brush;
		brush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
		dc->FillRect(CRect(m_rectRegion.right - SIZE_SCROLL, m_rectRegion.bottom - SIZE_SCROLL,
						m_rectRegion.right, m_rectRegion.bottom), &brush);
		brush.DeleteObject();
	}
}

void CTableRegion::UpgradeScroll(){
	int cx = m_tableDrawer.GetWidthTable();
	int sizeCyTable = m_rectRegion.Size().cy;
	int sizeCxTable = m_rectRegion.Size().cx;
	bool m_bResizeCx = false;
	if(sizeCyTable - SIZE_SCROLL < m_tableDrawer.GetHeightTable())
		m_bResizeCx = true;
	if(sizeCxTable - SIZE_SCROLL < cx)
		sizeCyTable -= SIZE_SCROLL;
	if(m_bResizeCx)
		sizeCxTable -= SIZE_SCROLL;
	if(sizeCxTable < cx)
		m_bHScroll = true;
	else
		m_bHScroll = false;
	if(sizeCyTable < m_tableDrawer.GetHeightTable())
		m_bVScroll = true;
	else
		m_bVScroll = false;

	if(m_bHScroll)
		m_HScroll.ShowWindow(SW_SHOW);
	else
		m_HScroll.ShowWindow(SW_HIDE);
	if(m_bVScroll)
		m_VScroll.ShowWindow(SW_SHOW);
	else
		m_VScroll.ShowWindow(SW_HIDE);
	if(m_bHScroll){
		SCROLLINFO si;
		m_HScroll.GetScrollInfo(&si);
		si.nPage = sizeCxTable;
		si.nMin = 0;
		si.nMax = cx;
		m_HScroll.SetScrollInfo(&si);
		m_HScroll.GetScrollInfo(&si);
		m_tableDrawer.SetPosX(-si.nPos);
	}
	if(m_bVScroll){
		SCROLLINFO si;
		m_VScroll.GetScrollInfo(&si);
		si.nPage = sizeCyTable;
		si.nMin = 0;
		si.nMax = m_tableDrawer.GetHeightTable();
		m_VScroll.SetScrollInfo(&si);
		m_VScroll.GetScrollInfo(&si);
		m_tableDrawer.UpgradeFirstVisiblePosY(si.nPos);
	}
	else{
		SCROLLINFO si;
		m_VScroll.GetScrollInfo(&si);
		si.nPos = 0;
		m_VScroll.SetScrollInfo(&si);
		m_tableDrawer.SetFirstCelPosY(0);
		m_tableDrawer.InitTable();
	}
	MoveScroll();
}

void CTableRegion::MoveScroll(){
	CRect rectHScroll(m_rectRegion.left, m_rectRegion.bottom - SIZE_SCROLL,	m_rectRegion.right, m_rectRegion.bottom);
	CRect rectVScroll(m_rectRegion.right - SIZE_SCROLL, m_rectRegion.top, m_rectRegion.right, m_rectRegion.bottom);
	if(m_bVScroll)
		rectHScroll.right -= SIZE_SCROLL;
	m_HScroll.MoveWindow(rectHScroll, TRUE);
	if(m_bHScroll)
		rectVScroll.bottom -= SIZE_SCROLL;
	m_VScroll.MoveWindow(rectVScroll, TRUE);
}

void CTableRegion::SetCursor(CPoint pos){
	if(m_tableDrawer.IsBeginResizeHeader(pos - m_rectRegion.TopLeft()))
		::SetCursor(LoadCursor(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_HSPLITBAR)));
	else if(m_tableDrawer.IsBeginResizeRow(pos - m_rectRegion.TopLeft()))
		::SetCursor(LoadCursor(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_VSPLITBAR)));
	else
		::SetCursor(LoadCursor(NULL, IDC_ARROW));
}

bool CTableRegion::IsResize(){
	return m_tableDrawer.IsResizeHeader() || m_tableDrawer.IsResizeRow();
}

void CTableRegion::Resize(){
	CPoint posCursor;
	GetCursorPos(&posCursor);
	CSize posDif = posCursor - m_nBeginPosResize;
	if(abs(posDif.cx) > 0 && m_tableDrawer.IsResizeHeader()){
		CSize sizeHeader = m_tableDrawer.GetResizeHeaderRect().Size();
		sizeHeader.cx += posDif.cx;
		if(sizeHeader.cx > 3)
			m_nBeginPosResize = posCursor;
		else{
			m_nBeginPosResize.x = posCursor.x - sizeHeader.cx + 3;
			sizeHeader.cx = 3;
		}
		CPoint posHeader = m_tableDrawer.GetResizeHeaderRect().TopLeft();
		CSize sizeHeader1 = m_tableDrawer.GetResizeHeaderRect().Size();
		CRect rect;
		m_pParent->GetWindowRect(rect);
		if(rect.left + posHeader.x + sizeHeader.cx != m_nBeginPosResize.x){
			m_nBeginPosResize.x = m_rectRegion.left + rect.left + posHeader.x + sizeHeader1.cx;
			SetCursorPos(m_nBeginPosResize.x, m_nBeginPosResize.y);
		}
		m_tableDrawer.ResizeHeader(sizeHeader);
	}
	if(abs(posDif.cy) > 0 && m_tableDrawer.IsResizeRow()){
		int sizeRow;
		m_tableDrawer.GetResizeRowPosSize(NULL, &sizeRow);
		sizeRow += posDif.cy;
		if(sizeRow > 3)
			m_nBeginPosResize = posCursor;
		else{
			m_nBeginPosResize.y = posCursor.y - sizeRow + 3;
			sizeRow = 3;
		}
		int posRow, rowSize;
		m_tableDrawer.GetResizeRowPosSize(&posRow, &rowSize);
		CRect rect;
		m_pParent->GetWindowRect(rect);
		if(rect.top + posRow + rowSize != m_nBeginPosResize.y){
			m_nBeginPosResize.y = m_rectRegion.top + rect.top + posRow + sizeRow;
			SetCursorPos(m_nBeginPosResize.x, m_nBeginPosResize.y);
		}
		m_tableDrawer.ResizeRow(sizeRow);
	}
	short s = GetKeyState(VK_LBUTTON);
	if(!HIWORD(s)){
		m_pParent->PostMessage(WM_SETCURSOR, 0, 0);
		if(m_tableDrawer.IsResizeHeader())
			m_tableDrawer.EndResizeHeader();
		if(m_tableDrawer.IsResizeRow())
			m_tableDrawer.EndResizeRow();
	}
}

bool CTableRegion::MouseLBtnDown(CPoint point){
	if(m_tableDrawer.BeginResizeHeader(point - m_rectRegion.TopLeft()))
		GetCursorPos(&m_nBeginPosResize);
	else if(m_tableDrawer.BeginResizeRow(point - m_rectRegion.TopLeft()))
		GetCursorPos(&m_nBeginPosResize);
	else{
		m_tableDrawer.SelectRow(point - m_rectRegion.TopLeft());
		return true;
	}
	return false;
}

bool CTableRegion::IsScroll(CScrollBar* pScroll){
	return pScroll == &m_HScroll || pScroll == &m_VScroll;
}

void CTableRegion::Scroll(UINT nSBCode, CScrollBar* pScroll){
	SCROLLINFO si;
	pScroll->GetScrollInfo(&si);
	if(nSBCode == SB_THUMBTRACK){
		si.nPos = si.nTrackPos;
	} else if(nSBCode == SB_LINEDOWN){
		si.nPos += 5;
	} else if(nSBCode == SB_LINEUP){
		si.nPos -= 5;
	} else if(nSBCode == SB_PAGEDOWN){
		si.nPos += si.nPage;
	} else if(nSBCode == SB_PAGEUP){
		si.nPos -= si.nPage;
	}
	pScroll->SetScrollInfo(&si);
	pScroll->GetScrollInfo(&si);
	if(pScroll == &m_VScroll)
		m_tableDrawer.UpgradeFirstVisiblePosY(si.nPos);
	else if(pScroll == &m_HScroll)
		m_tableDrawer.SetPosX(-si.nPos);
}

bool CTableRegion::IsSelectRow(){
	return m_tableDrawer.IsSelectRow();
}

CTableDrawer* CTableRegion::GetTableDrawer(){
	return &m_tableDrawer;
}